package jp.ac.u_tokyo.dch.action.csv;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jp.ac.u_tokyo.dch.core.db.DBConnection;
import jp.ac.u_tokyo.dch.dao.schema.DataSchemaDao;
import jp.ac.u_tokyo.dch.model.PageInfo;
import jp.ac.u_tokyo.dch.model.db.Data;
import jp.ac.u_tokyo.dch.model.db.DataSchema;
import jp.ac.u_tokyo.dch.service.DaoServiceAware;
import jp.ac.u_tokyo.dch.util.common.CsvPageUtil;

import com.opensymphony.xwork2.ActionSupport;

public class CsvAction extends ActionSupport implements DaoServiceAware {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	 /**
	 * DBコネクションオブジェクト
	 */
	private DBConnection dbConnection = null;

	/**
	 * 現在ページ
	 */
	private String currentPage = null;
	/**
	 * スキーマオプションリスト
	 */
	private List<DataSchema> optionList;

	/**
	 * ページ状態保存オブジェクト
	 */
	private PageInfo<Data> page;
	/**
	 * スキーマID
	 */
	private String schemaid = null;

	/**
	 * 表示件数
	 */
	private String viewPageCnt = null;
	/**
	 * データタイプ
	 */
	private String dataType = null;
	/**
	 * Tokenエラー
	 * @return
	 * @throws Exception
	 */
	public String main() throws Exception {

		return SUCCESS;
	}

	public String downloadInit() throws Exception {

		DataSchemaDao dataSchemaDao = new DataSchemaDao(dbConnection);

		optionList = dataSchemaDao.selectList(dataType);

		return SUCCESS;
	}

	public String uploadInit() throws Exception {

		return SUCCESS;
	}

	public String search() throws Exception {

		currentPage = "1";
		searchProcess();
		return SUCCESS;

	}

	public String searchstate() throws Exception {

		searchProcess();
		return SUCCESS;

	}


	public List<DataSchema> getOptionList() {
		return optionList;
	}

	public void setOptionList(List<DataSchema> optionList) {
		this.optionList = optionList;
	}

	public DBConnection getDbConnection() {
		return dbConnection;
	}

	public void setDbConnection(DBConnection dbConnection) {
		this.dbConnection = dbConnection;
	}

	public String getDataType() {
		return dataType;
	}

	public void setDataType(String dataType) {
		this.dataType = dataType;
	}

	public PageInfo<Data> getPage() {
		return page;
	}

	public void setPage(PageInfo<Data> page) {
		this.page = page;
	}

	public String getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(String currentPage) {
		this.currentPage = currentPage;
	}

	public String getViewPageCnt() {
		return viewPageCnt;
	}

	public void setViewPageCnt(String viewPageCnt) {
		this.viewPageCnt = viewPageCnt;
	}

	public String getSchemaid() {
		return schemaid;
	}

	public void setSchemaid(String schemaid) {
		this.schemaid = schemaid;
	}

	@Override
	public void setDBConnection(DBConnection con) {
		dbConnection = con;

	}
	/**
	 * 分類セット
	 * @param sort
	 * @return 分類
	 */
	public String getSortName(String sort) {

		return getText("outputmenu_" + sort);
	}
	/**
	 * 検索共通処理
	 * @throws Exception 例外
	 */
	private void searchProcess() throws Exception {

		CsvPageUtil pageUtil = new CsvPageUtil(dbConnection);
		DataSchemaDao dataSchemaDao = new DataSchemaDao(dbConnection);

		Map<String,String> searchKeyMap = new HashMap<String, String>();
		searchKeyMap.put("dataType", dataType);
		searchKeyMap.put("schemaid", schemaid);

		page = pageUtil.createPageInfo(currentPage, viewPageCnt, searchKeyMap);
		optionList = dataSchemaDao.selectList(dataType);
	}

}
